package com.weihui.jxc.view;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.weihui.jxc.common.CommonFactory;
import com.weihui.jxc.model.Operator;
import com.weihui.jxc.service.OperatorService;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Font;
import javax.swing.SwingConstants;
import javax.swing.ImageIcon;
import java.awt.SystemColor;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.UIManager;
import javax.swing.JPasswordField;
import java.awt.Color;

/**
 * 登录界面
 * 
 * @author xiaowei
 *
 */
public class LoginFrame extends JFrame implements ActionListener{

	private JPanel contentPane;
	private JTextField username;// 账号
	private JPasswordField password;// 密码
	private JButton submit;// 登录按钮
	private JButton cancel;// 取消按钮
	private static Operator operator;//登录成功之后，保留
	/**
	 * 绘制登录界面
	 */
	public LoginFrame() {
		setBackground(Color.LIGHT_GRAY);
		setTitle("进销存管理系统");
		setIconImage(Toolkit.getDefaultToolkit().getImage(LoginFrame.class.getResource("/images/管理系统.png")));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		setResizable(false);//不可更改大小
		setLocationRelativeTo(null);//居中显示
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);

		JLabel label = new JLabel("企业进销存管理系统");
		label.setForeground(new Color(0, 153, 204));
		label.setIcon(new ImageIcon(LoginFrame.class.getResource("/images/管理系统.png")));
		label.setHorizontalAlignment(SwingConstants.CENTER);
		label.setFont(new Font("宋体", Font.BOLD, 24));

		JPanel panel = new JPanel();

		JPanel panel_1 = new JPanel();
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addComponent(label, GroupLayout.DEFAULT_SIZE, 426, Short.MAX_VALUE)
				.addComponent(panel, GroupLayout.DEFAULT_SIZE, 426, Short.MAX_VALUE)
				.addComponent(panel_1, GroupLayout.DEFAULT_SIZE, 426, Short.MAX_VALUE));
		gl_contentPane.setVerticalGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
						.addComponent(label, GroupLayout.PREFERRED_SIZE, 89, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(panel, GroupLayout.PREFERRED_SIZE, 93, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 50, Short.MAX_VALUE).addContainerGap()));

		submit = new JButton("登录");
		submit.setForeground(new Color(0, 153, 204));
		submit.setIcon(new ImageIcon(LoginFrame.class.getResource("/images/登陆 (1).png")));
		submit.setBackground(UIManager.getColor("List.selectionBackground"));
		submit.setFont(new Font("宋体", Font.BOLD, 16));
	    submit.addActionListener(this);//给登录按钮绑定点击事件
	    
		cancel = new JButton("退出");
		cancel.setForeground(new Color(0, 153, 204));
		cancel.setIcon(new ImageIcon(LoginFrame.class.getResource("/images/退出.png")));
		cancel.setFont(new Font("宋体", Font.BOLD, 16));
		cancel.setBackground(UIManager.getColor("List.selectionBackground"));
		cancel.addActionListener(this);
		
		GroupLayout gl_panel_1 = new GroupLayout(panel_1);
		gl_panel_1.setHorizontalGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_1.createSequentialGroup().addGap(74).addComponent(submit)
						.addPreferredGap(ComponentPlacement.RELATED, 96, Short.MAX_VALUE).addComponent(cancel)
						.addGap(70)));
		gl_panel_1
				.setVerticalGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel_1.createSequentialGroup().addContainerGap()
								.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
										.addComponent(cancel, GroupLayout.PREFERRED_SIZE, 33,
												GroupLayout.PREFERRED_SIZE)
										.addComponent(submit))
								.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
		panel_1.setLayout(gl_panel_1);

		JLabel lblNewLabel = new JLabel("账号：");
		lblNewLabel.setForeground(new Color(0, 153, 204));
		lblNewLabel.setIcon(new ImageIcon(LoginFrame.class.getResource("/images/uname1.png")));
		lblNewLabel.setFont(new Font("宋体", Font.BOLD, 16));

		username = new JTextField();
		username.setFont(new Font("宋体", Font.PLAIN, 16));
		username.setToolTipText("请输入账号");
		username.setColumns(20);

		JLabel lblNewLabel_1 = new JLabel("密码：");
		lblNewLabel_1.setForeground(new Color(0, 153, 204));
		lblNewLabel_1.setIcon(new ImageIcon(LoginFrame.class.getResource("/images/pwd.png")));
		lblNewLabel_1.setFont(new Font("宋体", Font.BOLD, 16));

		password = new JPasswordField();
		password.setFont(new Font("宋体", Font.PLAIN, 16));
		password.setColumns(10);
		password.setEchoChar('*');// 星花显示
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(gl_panel.createParallelGroup(Alignment.LEADING).addGroup(gl_panel
				.createSequentialGroup().addGap(72)
				.addGroup(gl_panel.createParallelGroup(Alignment.LEADING, false)
						.addGroup(gl_panel.createSequentialGroup().addComponent(lblNewLabel_1).addGap(18)
								.addComponent(password))
						.addGroup(gl_panel.createSequentialGroup().addComponent(lblNewLabel).addGap(18)
								.addComponent(username, GroupLayout.PREFERRED_SIZE, 190, GroupLayout.PREFERRED_SIZE)))
				.addGap(70)));
		gl_panel.setVerticalGroup(gl_panel.createParallelGroup(Alignment.LEADING).addGroup(gl_panel
				.createSequentialGroup().addGap(8)
				.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(username, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
								GroupLayout.PREFERRED_SIZE)
						.addComponent(lblNewLabel))
				.addGap(18)
				.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE).addComponent(lblNewLabel_1).addComponent(
						password, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
				.addContainerGap(17, Short.MAX_VALUE)));
		panel.setLayout(gl_panel);
		contentPane.setLayout(gl_contentPane);

	}
	/**
	 * 点击事件
	 */
    @Override
    public void actionPerformed(ActionEvent e) {
    	String btnCode = e.getActionCommand();//获取到的就是按钮上的文字
    	switch (btnCode) {
		case "登录":
			String usernameStr = username.getText().trim();
			String passwordStr = String.valueOf(password.getPassword());
			if(usernameStr.length()==0) {
				JOptionPane.showMessageDialog(null, "用户名不能为空","警告",JOptionPane.WARNING_MESSAGE);
			}else if(passwordStr.trim().length()==0) {
				JOptionPane.showMessageDialog(null, "密码不能为空","警告",JOptionPane.WARNING_MESSAGE);
			}else {
				OperatorService service = CommonFactory.getOperatorService();  
				boolean isPass = service.loginCheck(new Operator(usernameStr, passwordStr));
				if(isPass) {
					MainFrame.username = usernameStr;
					MainFrame.operator = service.getOperator(usernameStr);
					//JOptionPane.showMessageDialog(null, "登录成功","警告",JOptionPane.INFORMATION_MESSAGE);
					MainFrame.getMainFrame().setVisible(true);//跳转主界面
					this.setVisible(false);//隐藏当前界面
				}else {
					JOptionPane.showMessageDialog(null, "登录失败，用户名或者密码错误！","警告",JOptionPane.WARNING_MESSAGE);
				}
			}
			break;
		case "退出":
			System.exit(0);
			break;
		default:
			break;
		}
    }
    /**
     * 访问登录后的用户
     * @return
     */
    public static Operator getLoginedUser() {
    	System.out.print("登陆信息："+operator);
    	return operator;
    }
}